/**
 * 
 */
package info.niwota.profile;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

/**
 * Pick and save image path
 * 
 * @author qiangli
 *
 */
public class PickPhotoActivity extends Activity {
	
	private static final String TAG = "PickPhotoActivity";

	private static final int RC_PICK = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
		try {
			Intent i = new Intent(Intent.ACTION_GET_CONTENT);
			i.setType("image/*");
			
			startActivityForResult(i, RC_PICK);
		} catch (Exception e) {
			// e.printStackTrace();
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult requestCode: " + requestCode + " resultCode: " + resultCode + " data: " +data);
		}
		if (resultCode == RESULT_OK && requestCode == RC_PICK) {

			setImagePath(data);
		}
		finish();
	}
	
	private void setImagePath(Intent data) {
		try {
			Uri uri = data.getData();
			if (uri != null) {
				Preferences.setProfilePhoto(this, uri.toString());

			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
	}
	
}
